package com.hs.dnms.action.search;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.db.QueryPage;
import com.hs.dnms.model.DnmsBook;
import com.hs.dnms.service.IDataService;
@Controller  
public class SearchController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(SearchController.class);
	private IDataService dataService;
    @Autowired  
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}
	 /**
	  * 搜索主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/sindex.do")   
     public ModelAndView sindex(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 List typelist = dbo.queryList("select firstclass,count(*) as countall from DNMS_BOOK  where firstclass is not null group by firstclass", null);
		 List catalist = dbo.queryList("select DISEASECLASS,count(*) as countall from DNMS_BOOK where DISEASECLASS is not null group by DISEASECLASS", null);
		 modelMap.put("typelist", typelist);
		 modelMap.put("catalist", catalist);
		 StringBuffer allfullsql = new StringBuffer(" select * from dnms_book where 1=1 ");
		 
		 allfullsql.append(createSql(request,"txt_1","ITEMNO")==null?"":createSql(request,"txt_1","ITEMNO"));
		 allfullsql.append(createSql(request,"txt_2","BOOKNAME")==null?"":createSql(request,"txt_2","BOOKNAME"));
		 allfullsql.append(createSql(request,"txt_3","diseasename")==null?"":createSql(request,"txt_3","diseasename"));
		 allfullsql.append(createSql(request,"txt_4","firstclass")==null?"":createSql(request,"txt_4","firstclass"));
		 allfullsql.append(createSql(request,"txt_5","originaltext")==null?"":createSql(request,"txt_5","originaltext"));
		 allfullsql.append(createSql(request,"txt_6","author")==null?"":createSql(request,"txt_6","author"));
		 allfullsql.append(createSql(request,"txt_7","compile")==null?"":createSql(request,"txt_7","compile"));
		 allfullsql.append(createSql(request,"txt_8","compileunit")==null?"":createSql(request,"txt_8","compileunit"));
		 allfullsql.append(createSql(request,"txt_9","publishhouse")==null?"":createSql(request,"txt_9","publishhouse"));
		 allfullsql.append(createSql(request,"txt_10","publishplace")==null?"":createSql(request,"txt_10","publishplace"));
		 allfullsql.append(createSql(request,"txt_11","publishdate")==null?"":createSql(request,"txt_11","publishdate"));
		 allfullsql.append(createSql(request,"txt_12","themewords")==null?"":createSql(request,"txt_12","themewords"));
		 allfullsql.append(createSql(request,"txt_13","abstract")==null?"":createSql(request,"txt_13","abstract"));
		 allfullsql.append(createSql(request,"txt_14","dynasty")==null?"":createSql(request,"txt_14","dynasty"));

		 System.out.println("allfullsql::" + allfullsql);
		 String perPageCount = request.getParameter("perPageCount");
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/sindex.do",allfullsql.toString()); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(8);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allsql", allfullsql.toString());
	     modelMap.put("searchtype", "1");
	     String statinfo = statinfo(request,allfullsql.toString());
	     logger.debug("statinfo:" +statinfo);
	     modelMap.put("statinfo", statinfo);
		 return new ModelAndView("app/search/resultlist",modelMap);
		 
	 } 
	 /**
	  * 搜索主页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/sindex3.do")   
     public ModelAndView sindex3(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 //排序
		 String orderName = request.getParameter("orderName");
		 String orderNum = request.getParameter("orderNum");
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("a.bookname").append(" desc");
		    	orderName = "a.bookname";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 List typelist = dbo.queryList("select firstclass,count(*) as countall from DNMS_BOOK  where firstclass is not null group by firstclass", null);
		 List catalist = dbo.queryList("select DISEASECLASS,count(*) as countall from DNMS_BOOK where DISEASECLASS is not null group by DISEASECLASS", null);
		 modelMap.put("typelist", typelist);
		 modelMap.put("catalist", catalist);
		 StringBuffer allfullsql = new StringBuffer(" select * from dnms_book a where 1=1 ");
		 String firstclass = request.getParameter("firstclass");
		 String diseaseclass = request.getParameter("diseaseclass");
		 if(null != firstclass && !"".equals(firstclass)){
			 allfullsql.append("and firstclass='");
			 allfullsql.append(firstclass);
			 allfullsql.append("'");
		 }
		 if(null != diseaseclass && !"".equals(diseaseclass)){
			 allfullsql.append("and diseaseclass='");
			 allfullsql.append(diseaseclass);
			 allfullsql.append("'");
		 }
		 allfullsql.append(createSql(request,"txt_1","ITEMNO")==null?"":createSql(request,"txt_1","ITEMNO"));
		 allfullsql.append(createSql(request,"txt_2","BOOKNAME")==null?"":createSql(request,"txt_2","BOOKNAME"));
		 allfullsql.append(createSql(request,"txt_3","diseasename")==null?"":createSql(request,"txt_3","diseasename"));
		 allfullsql.append(createSql(request,"txt_4","firstclass")==null?"":createSql(request,"txt_4","firstclass"));
		 allfullsql.append(createSql(request,"txt_5","originaltext")==null?"":createSql(request,"txt_5","originaltext"));
		 allfullsql.append(createSql(request,"txt_6","author")==null?"":createSql(request,"txt_6","author"));
		 allfullsql.append(createSql(request,"txt_7","compile")==null?"":createSql(request,"txt_7","compile"));
		 allfullsql.append(createSql(request,"txt_8","compileunit")==null?"":createSql(request,"txt_8","compileunit"));
		 allfullsql.append(createSql(request,"txt_9","publishhouse")==null?"":createSql(request,"txt_9","publishhouse"));
		 allfullsql.append(createSql(request,"txt_10","publishplace")==null?"":createSql(request,"txt_10","publishplace"));
		 allfullsql.append(createSql(request,"txt_11","publishdate")==null?"":createSql(request,"txt_11","publishdate"));
		 allfullsql.append(createSql(request,"txt_12","themewords")==null?"":createSql(request,"txt_12","themewords"));
		 allfullsql.append(createSql(request,"txt_13","abstract")==null?"":createSql(request,"txt_13","abstract"));
		 allfullsql.append(createSql(request,"txt_14","dynasty")==null?"":createSql(request,"txt_14","dynasty"));

		 System.out.println("allfullsql::" + allfullsql);
		 allfullsql.append(orderByStr);
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/sindex3.do",allfullsql.toString(),conditionmap); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(8);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";
	     List datalist = qp.getDataList();
	     for(int i=0; i<datalist.size(); i++){
	    	 Map onemap = (Map)datalist.get(i);
	    	 int bookid = Integer.parseInt(onemap.get("id")+"");
	    	 String dissql = "select a.moddistype,b.disname from dnms_dis_anc_mod a ,dnms_modern_dis b where a.modid = b.id and a.bookid=" + bookid;
	    	 List dislist = dbo.queryList(dissql, null);
	    	 String moddisen = "";
	    	 String moddiscn = "";
	    	 for(int j=0; j<dislist.size(); j++){
	    		 Map dismap = (Map)dislist.get(j);
	    		 String distype = dismap.get("moddistype")+"";
	    		 String disname = dismap.get("disname")+"";
	    		 if("1".equals(distype)){
	    			 moddiscn = moddiscn + disname + "、";
	    		 }else if("2".equals(distype)){
	    			 moddisen = moddisen + disname + "、";
	    		 }
	    	 }
	    	 if(moddiscn.endsWith("、")){
    			 moddiscn = moddiscn.substring(0,moddiscn.length()-1);
    		 }
    		 if(moddisen.endsWith("、")){
    			 moddisen = moddisen.substring(0,moddisen.length()-1);
    		 }
	    	 onemap.put("moddisen", moddisen);
	    	 onemap.put("moddiscn", moddiscn);
	     }
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allsql", allfullsql.toString());
	     modelMap.put("searchtype", "1");
	     String statinfo = statinfo(request,allfullsql.toString());
	     logger.debug("statinfo:" +statinfo);
	     modelMap.put("statinfo", statinfo);
	     
		 return new ModelAndView("app/search/booklist",modelMap);
		 
	 } 
	 private String createSql(HttpServletRequest request,String cname,String dbname){
		 StringBuffer sqlbuffer = new StringBuffer();
		 //值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 request.setAttribute(c_value, cvalue);
		 request.setAttribute(c_logical, c_logical);
		 request.setAttribute(c_sel, c_sel);
		 request.setAttribute(c_special, c_special);
		 System.out.println("value:::" + clogical + "," + csel + "," + cvalue + "," + cspecial);
//		 if(StringUtils.isEmpty(cspecial))
//			 cspecial = "1";//默认为模糊检索
		 if(null == cvalue || StringUtils.isEmpty(cvalue)){//查询条件为空时返回空
			 return null;
		 }
		 if("0".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" and "); 
		 }else if("1".equals(clogical)){
			 //0 and 关系  1：或者关系
			 sqlbuffer.append(" or "); 
		 }else{
			 sqlbuffer.append(" and "); 
		 }
		 sqlbuffer.append(csel);
		 if("0".equals(cspecial)){
			 //0 精确  1：模糊
			 sqlbuffer.append(" ='"); 
			 sqlbuffer.append(cvalue);
			 sqlbuffer.append("'");
		 }
		 if("1".equals(cspecial)){
			//0 精确  1：模糊
			 sqlbuffer.append(" like '%"); 
			 sqlbuffer.append(cvalue);
			 sqlbuffer.append("%'");
		 }
		 return sqlbuffer.toString();
	 }
	
	 /**
	  * 查询提交action
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/doSearch.do")   
     public ModelAndView doSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
		 //排序
		 String orderName = request.getParameter("orderName");
		 String orderNum = request.getParameter("orderNum");
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("a.bookname").append(" desc");
		    	orderName = "a.bookname";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("orderNum", orderNum);
		 StringBuffer allfullsql = new StringBuffer("select * from dnms_book a where 1=1 ");
		 String firstclass = request.getParameter("firstclass");
		 String diseaseclass = request.getParameter("diseaseclass");
		 if(null != firstclass && !"".equals(firstclass)){
			 allfullsql.append("and firstclass='");
			 allfullsql.append(firstclass);
			 allfullsql.append("'");
		 }
		 if(null != diseaseclass && !"".equals(diseaseclass)){
			 allfullsql.append("and diseaseclass='");
			 allfullsql.append(diseaseclass);
			 allfullsql.append("'");
		 }
		 
         allfullsql.append(specialSql(request,"pro_1")==null?"":specialSql(request,"pro_1"));
         allfullsql.append(specialSql(request,"pro_2")==null?"":specialSql(request,"pro_2"));
         allfullsql.append(specialSql(request,"pro_3")==null?"":specialSql(request,"pro_3"));
         allfullsql.append(specialSql(request,"pro_4")==null?"":specialSql(request,"pro_4"));
         allfullsql.append(specialSql(request,"pro_5")==null?"":specialSql(request,"pro_5"));
         allfullsql.append(specialSql(request,"pro_6")==null?"":specialSql(request,"pro_6"));
         allfullsql.append(specialSql(request,"pro_7")==null?"":specialSql(request,"pro_7"));
         allfullsql.append(specialSql(request,"pro_8")==null?"":specialSql(request,"pro_8"));

		 //allfullsql.append(")");
		 
		 allfullsql.append(createSql(request,"spec_1","ITEMNO")==null?"":createSql(request,"spec_1","ITEMNO"));
		 allfullsql.append(createSql(request,"spec_2","BOOKNAME")==null?"":createSql(request,"spec_2","BOOKNAME"));
		 allfullsql.append(createSql(request,"spec_3","diseasename")==null?"":createSql(request,"spec_3","diseasename"));
		 allfullsql.append(createSql(request,"spec_4","firstclass")==null?"":createSql(request,"spec_4","firstclass"));
		 allfullsql.append(createSql(request,"spec_5","originaltext")==null?"":createSql(request,"spec_5","originaltext"));
		 allfullsql.append(createSql(request,"spec_6","author")==null?"":createSql(request,"spec_6","author"));
		 allfullsql.append(createSql(request,"spec_7","compile")==null?"":createSql(request,"spec_7","compile"));
		 allfullsql.append(createSql(request,"spec_8","compileunit")==null?"":createSql(request,"spec_8","compileunit"));
		 allfullsql.append(createSql(request,"spec_9","publishhouse")==null?"":createSql(request,"spec_9","publishhouse"));
		 allfullsql.append(createSql(request,"spec_10","publishplace")==null?"":createSql(request,"spec_10","publishplace"));
		 allfullsql.append(createSql(request,"spec_11","publishdate")==null?"":createSql(request,"spec_11","publishdate"));
		 allfullsql.append(createSql(request,"spec_12","themewords")==null?"":createSql(request,"spec_12","themewords"));
		 allfullsql.append(createSql(request,"spec_13","abstract")==null?"":createSql(request,"spec_13","abstract"));
		 allfullsql.append(createSql(request,"spec_14","dynasty")==null?"":createSql(request,"spec_14","dynasty"));
		 
		 
		 System.out.println("allfullsql::::: "+ allfullsql.toString());
		 allfullsql.append(orderByStr);
		 String perPageCount = request.getParameter("perPageCount");
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/search/doSearch.do",allfullsql.toString(),conditionmap); 
	     
//	     if(null == perPageCount || "".equals(perPageCount)){
//				qp.setPerPageCount(8);
//			 }else {
//				qp.setPerPageCount(Integer.parseInt(perPageCount));
//			 }
		 qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + "";	    
	     List datalist = qp.getDataList();
	     for(int i=0; i<datalist.size(); i++){
	    	 Map onemap = (Map)datalist.get(i);
	    	 int bookid = Integer.parseInt(onemap.get("id")+"");
	    	 String dissql = "select a.moddistype,b.disname from dnms_dis_anc_mod a ,dnms_modern_dis b where a.modid = b.id and a.bookid=" + bookid;
	    	 List dislist = dbo.queryList(dissql, null);
	    	 String moddisen = "";
	    	 String moddiscn = "";
	    	 for(int j=0; j<dislist.size(); j++){
	    		 Map dismap = (Map)dislist.get(j);
	    		 String distype = dismap.get("moddistype")+"";
	    		 String disname = dismap.get("disname")+"";
	    		 if("1".equals(distype)){
	    			 moddiscn = moddiscn + disname + "、";
	    		 }else if("2".equals(distype)){
	    			 moddisen = moddisen + disname + "、";
	    		 }
	    	 }
	    	 if(moddiscn.endsWith("、")){
    			 moddiscn = moddiscn.substring(0,moddiscn.length()-1);
    		 }
    		 if(moddisen.endsWith("、")){
    			 moddisen = moddisen.substring(0,moddisen.length()-1);
    		 }
	    	 onemap.put("moddisen", moddisen);
	    	 onemap.put("moddiscn", moddiscn);
	     }
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allsql", allfullsql.toString());
	     String statinfo = statinfo(request,allfullsql.toString());
	     logger.debug("statinfo:" +statinfo);
	     modelMap.put("statinfo", statinfo);
	     modelMap.put("searchtype", "2");
	     
	     System.out.println("statinfo:" + statinfo);
		 return new ModelAndView("app/search/booklist",modelMap);
		 
	 } 
	 public String statinfo(HttpServletRequest request,String allsql){
		 StringBuffer buffer = new StringBuffer("共检索到");
		 
		 //检索出多少篇文章，涉及证候多少个，方剂多少种，中药多少种，穴位多少种。
		 DBMgr dbo = this.getDbo(request);
		 List symlist = dbo.queryList("select count(*) as countall from (select count(*) ,SYMPTOMNAME from DNMS_SYM where disid  in(select id from DNMS_DISEASE where BOOKID in(select id from ("+allsql+"))) group by SYMPTOMNAME)", null);
		 List pointlist = dbo.queryList("select count(*) as countall from (select count(*) , POINTNAME from DNMS_POINT where BOOKID  in(select id from ("+allsql+")) group by POINTNAME)",null); 
		 List prelist = dbo.queryList("select count(*) as countall from (select count(*) ,PRENAME from DNMS_PRE where BOOKID in(select id from ("+allsql+")) group by PRENAME)",null);
		 List herblist = dbo.queryList("select count(*) as countall from (SELECT count(*) ,HERBNAME FROM DNMS_HERB WHERE  BOOKID in(select id from ("+allsql+")) group by HERBNAME)",null);
		 List magalist = dbo.queryList("select to_char(count(*)) as paprenum from ("+allsql+")", null);
		 Map symmap = (Map)symlist.get(0);
		 Map pointmap = (Map)pointlist.get(0);
		 Map premap = (Map)prelist.get(0);
		 Map herbmap = (Map)herblist.get(0);
 		 Map magamap = (Map)magalist.get(0); 
		 buffer.append(magamap.get("paprenum")).append("篇文献，");
		 buffer.append("涉及的证候共").append(symmap.get("countall")+"个,");
		 buffer.append("方剂").append(premap.get("countall")+"种,");
		 buffer.append("中药").append(herbmap.get("countall")+"种,");
		 buffer.append("穴位").append(pointmap.get("countall")+"种！");

		 return buffer.toString();
	 }
	 
	 public String specialSql(HttpServletRequest request,String cname){
		 StringBuffer sbsql = new StringBuffer();
		//值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
         if(null != csel && !"".equals(csel)){
        	 if("diseasename".equals(csel)){
        		 //古籍中医疾病
        		 String jibingsql =  this.sql4jibing(cname,request);
        		 if(null != jibingsql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( jibingsql);
        			 else 
        				 sbsql.append(" and id in").append( jibingsql);
        		 }
        	 }else if("modisnameen".equals(csel)){
        		 //现代西医
        		 String xiyisql = this.sql4xiyi(cname,request);
        		 if(null != xiyisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( xiyisql);
        			 else 
        				 sbsql.append(" and id in").append( xiyisql);
        		 }
        		 System.out.println("sbsql::" + sbsql.toString());
        	 }else if("modisnamecn".equals(csel)){
        		 //现代中医
        		 String zhongyisql = this.sql4zhongyi(cname,request);
        		 if(null != zhongyisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( zhongyisql);
        			 else
        				 sbsql.append(" and id in").append( zhongyisql);
        		 }
        	 }else if("symptomname".equals(csel)){
        		 //证候
        		 String zhenghousql = this.sql4zhenghou(cname,request);
        		 if(null != zhenghousql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( zhenghousql);
        			 else
        				 sbsql.append(" and id in").append( zhenghousql);
        		 }
        	 }else if("prename".equals(csel)){
        		//方剂
        		 String fangjisql =  this.sql4fangji(cname,request);
        		 if(null != fangjisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( fangjisql);
        			 else
        				 sbsql.append(" and id in").append( fangjisql);
        		 }
        			 
        	 }else if("herbname".equals(csel)){
        		//中药
        		 String zhongyaosql = this.sql4zhongyao(cname,request);
        		 if(null != zhongyaosql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( zhongyaosql);
        			 else
        				 sbsql.append(" and id in").append( zhongyaosql);
        		 }
        	 }else if("pgroupname".equals(csel)){
        		 //针灸组
        		 String xueweizusql = this.sql4xueweizu(cname,request);
        		 if(null != xueweizusql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( xueweizusql);
        			 else
        				 sbsql.append(" and id in").append( xueweizusql);
        		 }
        	 }else if("pointname".equals(csel)){
        		//穴位
        		 String xuewei_r = request.getParameter("xuewei_r");
        		 String xueweisql = this.sql4xuewei(cname,request);
        		 if(null != xueweisql){
        			 if("1".equals(clogical))
        				 sbsql.append(" or id in").append( xueweisql);
        			 else
        				 sbsql.append(" and id in").append( xueweisql);
        		 }
        	 }
         }
         return sbsql.toString();
	 }
	 /**
	  * 古籍中医疾病查询
	  * @param request
	  * @return
	  */
	 private String sql4jibing(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer(" (select bookid from DNMS_DISEASE d where ");
		// String cname = "pro_1";
		//值
		 String c_value = cname+"_value";
		 //逻辑关系，并且 或者
		 String c_logical = cname+"_logical";
		 //字段的名字
		 String c_sel = cname+"_sel";
		 //精确或模糊
		 String c_special = cname+"_special";
		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
		 String clogical = request.getParameter(c_logical);
		 String csel = request.getParameter(c_sel);
		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);

		 if(!csel.equals("diseasename")){
			 return null;
		 }
		 if(StringUtils.isEmpty(cvalue) ){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" DISEASENAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" DISEASENAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" diseasename in(select standardname from dnms_standard where category='疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" diseasename in(select standardname from dnms_standard where category='疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append("))");
			 }
			 
		 }
		 
		 
       return buffer.toString();
	 }
	 
	 /**
	  * 现代西医查询
	  * @param request
	  * @return
	  */
	 private String sql4xiyi(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_DIS_ANC_MOD where moddistype='2' and modid in(select id from DNMS_MODERN_DIS where distype='2' and ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
 		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);

 		if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" disname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" disname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append("))");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" disname in(select standardname from dnms_standard where category='现代西医疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" disname in(select standardname from dnms_standard where category='现代西医疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append(")))");
			 }
			 
		 }
		 
		 return buffer.toString();
		 
	 }
	 /**
	  * 现代中医查询
	  * @param request
	  * @return
	  */
	 private String sql4zhongyi(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_DIS_ANC_MOD where moddistype='1' and modid in(select id from DNMS_MODERN_DIS where distype='1' and ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
 		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" disname ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" disname like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append("))");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" disname in(select standardname from dnms_standard where category='现代中医疾病' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" disname in(select standardname from dnms_standard where category='现代中医疾病' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append(")))");
			 }
		 }
		 return buffer.toString();
		 
	 }
	 /**
	  * 方剂查询
	  * @param request
	  * @return
	  */
	 private String sql4fangji(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_PRE p where ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
 		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);;
		 
		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PRENAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PRENAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PRENAME in(select standardname from dnms_standard where category='方剂' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PRENAME in(select standardname from dnms_standard where category='方剂' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 证候查询
	  * @param request
	  * @return
	  */
	 private String sql4zhenghou(String cname ,HttpServletRequest request){
 		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_SYM where  ");
 		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select standardname from dnms_standard where category='证候' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" SYMPTOMNAME in(select standardname from dnms_standard where category='证候' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 中药查询
	  * @param request
	  * @return
	  */
	 private String sql4zhongyao(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_HERB h where   ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		 if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" HERBNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" HERBNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" HERBNAME in(select standardname from dnms_standard where category='中药' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" HERBNAME in(select standardname from dnms_standard where category='中药' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
	 }
	 /**
	  * 针灸组查询
	  * @param request
	  * @return
	  */
	 private String sql4xueweizu(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_POINTGROUP where  ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PGROUPNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PGROUPNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select standardname from dnms_standard where category='针灸组' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" PGROUPNAME in(select standardname from dnms_standard where category='针灸组' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
		 
	 }
	 /**
	  * 穴位查询
	  * @param request
	  * @return
	  */
	 private String sql4xuewei(String cname ,HttpServletRequest request){
		 StringBuffer buffer = new StringBuffer("(select bookid from DNMS_POINT t where   ");
		//值
 		 String c_value = cname+"_value";
 		 //逻辑关系，并且 或者
 		 String c_logical = cname+"_logical";
 		 //字段的名字
 		 String c_sel = cname+"_sel";
 		 //精确或模糊
 		 String c_special = cname+"_special";
 		//正名或异名查询
		 String c_syn = cname+"_syn";
		 
		 String cvalue = request.getParameter(c_value);
 		 String clogical = request.getParameter(c_logical);
 		 String csel = request.getParameter(c_sel);
 		 String cspecial = request.getParameter(c_special);
		 String csyn = request.getParameter(c_syn);
 		if(StringUtils.isEmpty(cvalue)){
			 return null;
		 }
		 if(StringUtils.isNotEmpty(cvalue)){
			 if("0".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" POINTNAME ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" POINTNAME like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 }
				 buffer.append(")");
			 }else if("1".equals(csyn)){
				 if("0".equals(cspecial)){
					 buffer.append(" POINTNAME in(select standardname from dnms_standard where category='穴位' and synonyms ='");
					 buffer.append(cvalue);
					 buffer.append("' ");
				 }
				 if("1".equals(cspecial)){
					 buffer.append(" POINTNAME in(select standardname from dnms_standard where category='穴位' and synonyms like '%");
					 buffer.append(cvalue);
					 buffer.append("%' ");
				 } 
				 buffer.append("))");
			 }
		 }
		 return buffer.toString();
		 
	 }
	 
	 /**
	  *原文下载
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/search/filelist.do")   
     public ModelAndView filelist(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         String id = request.getParameter("id");
         DnmsBook dm = (DnmsBook)dataService.findByID(DnmsBook.class, new Long(id));
         modelMap.put("obj",dm);
 		 return new ModelAndView("app/search/filelist",modelMap);
		 
	 } 
}
